package com.limao.Tool;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class SMSHuyi {
	
	private static String url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
	private static String account = "look22";
	private static String password = "cvbvsgeb895";
	private static String sin0="验证码：";
	private static String sin1="，尊敬的用户，您好，请在页面输入以完成验证。";
	private static String sin2="[买到家]";//"【桂林市“买到家”购物平台】";

	public static boolean send(String mobile,String code)
	{
		boolean success=false;
		
		HttpClient client = new HttpClient(); 
		PostMethod method = new PostMethod(url); 
		client.getParams().setContentCharset("UTF-8");
		method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");
		
		String content = sin0+code+sin1+sin2;
		NameValuePair[] data = {
			    new NameValuePair("account", account), 
			    new NameValuePair("password", password), //密码可以使用明文密码或使用32位MD5加密
			    //new NameValuePair("password", StringUtil.MD5Encode(password)),
			    new NameValuePair("mobile", mobile), 
			    new NameValuePair("content", content),
		};
		method.setRequestBody(data);
		
		try {
			client.executeMethod(method);	
			
			String SubmitResult =method.getResponseBodyAsString();
			Document doc = DocumentHelper.parseText(SubmitResult); 
			Element root = doc.getRootElement();
			String result = root.elementText("code");	
			String msg = root.elementText("msg");	
						
			if(result == "2"){
				success = true;
				System.out.println(msg);
			}else{
				success = false;
				System.out.println("互亿短信发送失败 code:"+result+" msg: "+msg);
			}
			
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		return success;
	}
}
